package com.ss.android.common.location;

import android.content.Context;
import android.location.LocationManager;
import android.os.Message;

import com.bytedance.common.utility.Logger;
import com.bytedance.frameworks.baselib.network.http.exception.HttpResponseException;
import com.ss.android.common.util.CommonConstants;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.http.legacy.a.NameValueImpl;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

final class g implements Runnable {
    private final LocationUploadHelper b;
    private final long a;

    g(LocationUploadHelper arg1, long arg2) {
        this.b = arg1;
        this.a = arg2;

    }

    public final void run() {
        LocationUploadHelper v0_7;
        JSONObject v1_1;
        ArrayList v0_5;
        int v0_4;
        JSONObject v4;
        JSONObject v3;
        JSONObject v0 = null;
        int v1 = 1;
        try {
            if ((this.b.h) && (this.b.a.a(this.b.j)) || (this.b.n)) {
                v3 = this.b.a.d();
            } else {
                v3 = v0;
            }
            if ((this.b.i) && (this.b.b.a(this.b.j)) || (this.b.n)) {
                v0 = this.b.b.a();
            }

            v4 = new JSONObject();
            LocationUploadHelper.c();
            LocationUploadHelper.a(v3);
            LocationUploadHelper.a(v0);
            JSONObject v5 = new JSONObject();
            v5.put("cmd", this.b.c.a.getInt("last_alert_command", 0));
            v5.put("last_interval", (this.a - this.b.c.a.getLong("last_alert_time", 0)) / 1000);
            v5.put("loc_id", this.b.c.a());
            v5.put("op_type", this.b.c.a.getInt("last_op_command", 0));
            v5.put("op_time", this.b.c.a.getLong("last_op_time", 0) / 1000);
            v5.put("last_time", this.b.j / 1000);
            v4.put("location_feedback", v5);
            v4.put("sys_location", v3);
            v4.put("baidu_location", null);
            v4.put("amap_location", v0);
            v4.put("base_station", this.b.d.getBaseStationData());
            Object v0_3 = this.b.f.getSystemService(Context.LOCATION_SERVICE);
            if (v0_3 != null) {
                List v3_1 = ((LocationManager) v0_3).getProviders(true);
                if (v3_1 != null && !v3_1.isEmpty()) {
                    v4.put("location_setting", 1);
                    if (!((LocationManager) v0_3).isProviderEnabled("gps")) {
                        v1 = 0;
                    }
                    if (((LocationManager) v0_3).isProviderEnabled("network")) {
                        v1 |= 2;
                    }

                    if (((LocationManager) v0_3).isProviderEnabled("passive")) {
                        v0_4 = v1 | 4;
                    } else {
                        v0_4 = v1;
                    }
                    v4.put("location_mode", v0_4);
                } else {
                    v4.put("location_setting", 0);
                }
            }
            v0_5 = new ArrayList();
            ((List) v0_5).add(new NameValueImpl("dwinfo", LocationUploadHelper.b(v4)));
            v1 = 20480;
            String v0_6 = NetworkUtils.executePost(v1, CommonConstants.LOCATION_UPLOAD_URL, ((List) v0_5));
            if (android.support.design.a.isTextEmpty(v0_6)) {
            } else {
                v1_1 = new JSONObject(v0_6);
                if (v1_1.optInt("err_no") != 0) {
                } else {
                    v0_7 = this.b;
                    v1_1 = v1_1.optJSONObject("data");
                    if (v1_1 == null) {
                    } else {
                        int v3_2 = v1_1.optInt("cmd");
                        String v4_1 = v1_1.optString("curr_city");
                        String v5_1 = v1_1.optString("alert_title");
                        v0_7.c.a(v1_1.optString("loc_id"));
                        LocationUploadHelper.f$a v1_2 = new LocationUploadHelper.f$a((byte) 0);
                        v1_2.a = v3_2;
                        v1_2.b = v4_1;
                        v1_2.c = v5_1;
                        Message v3_3 = v0_7.e.obtainMessage(5);
                        v3_3.obj = v1_2;
                        v0_7.e.sendMessage(v3_3);
                    }
                    this.b.j = this.a;
                    this.b.c.b(0, 0);
                }
            }
            this.b.g = false;
            return;
        } catch (Exception v0_1) {
            Logger.d("LocationUploadHelper", "Internet exception:" + v0_1.toString());
            if (!(v0_1 instanceof HttpResponseException)) {
                v0_7 = this.b;
                long v4_2 = this.a;
                if (v4_2 > v0_7.p + 900000) {
                    v0_7.n = true;
                    v0_7.o = 0;
                    v0_7.p = v4_2;
                }

                if (v0_7.o < 3) {
                    ++v0_7.o;
                    v0_7.n = true;
                } else {
                    v0_7.n = false;
                }
            }
            this.b.g = false;
            return;
        }
    }
}

